package test.model.table;

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import test.model.ContactDAO;

public class ImageRenderer extends Canvas implements TableCellRenderer{

	private BufferedImage image = null;
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		
		try {
			
			if(value != null && value.toString().length()>0){
				String path = ContactDAO.getPath() + "images/" + (String)value;
				System.out.println("ImageRenderer: " + path + "; " + (String)value);
				//if(image == null)
					image = ImageIO.read(new File(path));
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(isSelected){
			
		}
		
		return this;
	}
	
	@Override
	public void paint(Graphics g){
		if(image != null){
			g.drawImage(image, 1, 1, getWidth()-2, getHeight()-2, null);
		}
	}

}
